home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / ufw / ufw-init-functions < prev    next >
Encoding:
Text File  |  2009-04-03  |  13.7 KB  |  378 lines

  1. #!/bin/sh -e
  2.  
  3. # ufw-init-functions: functions used by ufw-init and distribution initscripts
  4. #
  5. # Copyright (C) 2008-2009 Canonical Ltd.
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License version 3,
  9. #    as published by the Free Software Foundation.
  10. #
  11. #    This program is distributed in the hope that it will be useful,
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #    GNU General Public License for more details.
  15. #
  16. #    You should have received a copy of the GNU General Public License
  17. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19.  
  20. PATH="/sbin:/bin:/usr/sbin:/usr/bin"
  21.  
  22. for s in "/etc/default/ufw" "/etc/ufw/ufw.conf" ; do
  23.     if [ -s "$s" ]; then
  24.         . "$s"
  25.     else
  26.         echo "Could not find $s (aborting)"
  27.         exit 1
  28.     fi
  29. done
  30.  
  31. RULES_PATH="/etc/ufw"
  32. USER_PATH="/var/lib/ufw"
  33.  
  34. flush_builtins() {
  35.     error=""
  36.     execs="iptables"
  37.     if ip6tables -L INPUT -n >/dev/null 2>&1; then
  38.         execs="$execs ip6tables"
  39.     fi
  40.  
  41.     for exe in $execs
  42.     do
  43.         $exe -F || error="yes"
  44.         $exe -X || error="yes"
  45.         $exe -P INPUT ACCEPT || error="yes"
  46.         $exe -P OUTPUT ACCEPT || error="yes"
  47.         $exe -P FORWARD ACCEPT || error="yes"
  48.  
  49.         # now handle the mangle table
  50.         if $exe -t mangle -L -n >/dev/null 2>&1; then
  51.             for i in INPUT OUTPUT FORWARD PREROUTING POSTROUTING ; do
  52.                 $exe -t mangle -F $i || error="yes"
  53.                 $exe -t mangle -P $i ACCEPT || error="yes"
  54.             done
  55.         fi
  56.     done
  57.  
  58.     # now handle the nat table
  59.     if iptables -t nat -L -n >/dev/null 2>&1; then
  60.         for i in OUTPUT PREROUTING POSTROUTING ; do
  61.             iptables -t nat -F $i || error="yes"
  62.             iptables -t nat -P $i ACCEPT || error="yes"
  63.         done
  64.     fi
  65.  
  66.     if [ "$error" = "yes" ]; then
  67.         return 1
  68.     fi
  69. }
  70.  
  71. chains_command() {
  72.     flag="$1"
  73.     type=""
  74.     exe="iptables"
  75.     if [ "$2" = "6" ]; then
  76.         type="$2"
  77.         exe="ip6tables"
  78.     fi
  79.  
  80.     for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-limit-accept ufw$type-user-limit ufw$type-reject-input ufw$type-after-logging-input ufw$type-after-input ufw$type-user-input ufw$type-before-input ufw$type-before-logging-input ufw$type-reject-forward ufw$type-after-logging-forward ufw$type-after-forward ufw$type-user-logging-forward ufw$type-user-forward ufw$type-before-forward ufw$type-before-logging-forward ufw$type-reject-output ufw$type-after-logging-output ufw$type-after-output ufw$type-user-logging-output ufw$type-user-output ufw$type-before-output ufw$type-before-logging-output; do
  81.         if [ "$UFW_INIT_DEBUG" = "yes" ]; then
  82.             echo "$exe $flag $c" >&2
  83.             $exe $flag $c || true
  84.         else
  85.             $exe $flag $c 2>/dev/null || true
  86.         fi
  87.     done
  88. }
  89.  
  90. delete_chains() {
  91.     chains_command -F $1
  92.     chains_command -Z $1
  93.  
  94.     # Delete the secondary chains to reduce clutter, but keep the primary ones
  95.     # so that the primary chains don't leave the built-in chains just to come
  96.     # back later in a different place. This means that some (empty) chains will
  97.     # linger until the next boot after disabling ufw.
  98.     for c in ufw$type-logging-deny ufw$type-logging-allow ufw$type-not-local ufw$type-user-logging-input ufw$type-user-logging-output ufw$type-user-logging-forward ufw$type-user-limit-accept ufw$type-user-limit ufw$type-user-input ufw$type-user-forward ufw$type-user-output ; do
  99.         if [ "$UFW_INIT_DEBUG" = "yes" ]; then
  100.             echo "$exe $flag $c" >&2
  101.             $exe -X $c || true
  102.         else
  103.             $exe -X $c 2>/dev/null || true
  104.         fi
  105.     done
  106. }
  107.  
  108. ufw_start() {
  109.     out=""
  110.     if iptables -L ufw-user-input -n >/dev/null 2>&1 ; then
  111.         echo "Firewall already started, use 'force-reload'"
  112.         return 0
  113.     fi
  114.     if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
  115.         for m in $IPT_MODULES
  116.         do
  117.             modprobe $m || true
  118.         done
  119.  
  120.         if [ "$MANAGE_BUILTINS" = "yes" ]; then
  121.             flush_builtins
  122.         fi
  123.  
  124.         execs="iptables"
  125.  
  126.         # IPv6 setup
  127.         if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
  128.             if ip6tables -L INPUT -n >/dev/null 2>&1; then
  129.                 execs="$execs ip6tables"
  130.             else
  131.                 out="${out}\nProblem loading ipv6 (skipping)"
  132.             fi
  133.         else
  134.             if ip6tables -L INPUT -n >/dev/null 2>&1; then
  135.                 # IPv6 support disabled but available in the kernel, so
  136.                 # default DROP and accept all on loopback
  137.                 delete_chains 6 || error="yes"
  138.  
  139.                 ip6tables -P INPUT DROP || error="yes"
  140.                 ip6tables -P OUTPUT DROP || error="yes"
  141.                 ip6tables -P FORWARD DROP || error="yes"
  142.  
  143.                 # delete these first so don't add multiple rules
  144.                 ip6tables -D INPUT -i lo -j ACCEPT 2>/dev/null || true
  145.                 ip6tables -D OUTPUT -o lo -j ACCEPT 2>/dev/null || true
  146.  
  147.                 ip6tables -A INPUT -i lo -j ACCEPT || error="yes"
  148.                 ip6tables -A OUTPUT -o lo -j ACCEPT || error="yes"
  149.  
  150.                 if [ "$error" = "yes" ]; then
  151.                     out="${out}\nProblem loading ipv6 (skipping)"
  152.                 fi
  153.             fi
  154.         fi
  155.  
  156.         for exe in $execs
  157.         do
  158.             type=""
  159.             if [ "$exe" = "ip6tables" ]; then
  160.                 type="6"
  161.             fi
  162.             BEFORE_RULES="$RULES_PATH/before${type}.rules"
  163.             AFTER_RULES="$RULES_PATH/after${type}.rules"
  164.             USER_RULES="$USER_PATH/user${type}.rules"
  165.  
  166.             # flush the chains
  167.             delete_chains $type || error="yes"
  168.  
  169.             # setup built-in chains' default policy
  170.             if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
  171.                 $exe -P INPUT DROP || error="yes"
  172.             else
  173.                 $exe -P INPUT $DEFAULT_INPUT_POLICY || error="yes"
  174.             fi
  175.             if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
  176.                 $exe -P OUTPUT DROP || error="yes"
  177.             else
  178.                 $exe -P OUTPUT $DEFAULT_OUTPUT_POLICY || error="yes"
  179.             fi
  180.             if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
  181.                 $exe -P FORWARD DROP || error="yes"
  182.             else
  183.                 $exe -P FORWARD $DEFAULT_FORWARD_POLICY || error="yes"
  184.             fi
  185.  
  186.             # setup some other chains that can be used later
  187.             if [ "$type" != "6" ]; then
  188.                 $exe -N ufw${type}-not-local || error="yes"
  189.             fi
  190.  
  191.             # setup ufw${type}-logging-* chains
  192.             if ! $exe -L ufw${type}-logging-deny -n >/dev/null 2>&1 ; then
  193.                 $exe -N ufw${type}-logging-deny || error="yes"
  194.                 $exe -N ufw${type}-logging-allow || error="yes"
  195.             fi
  196.  
  197.             # setup ufw${type}-user-logging-* chains
  198.             if ! $exe -L ufw${type}-user-logging-input -n >/dev/null 2>&1 ; then
  199.                 $exe -N ufw${type}-user-logging-input || error="yes"
  200.                 $exe -N ufw${type}-user-logging-output || error="yes"
  201.                 $exe -N ufw${type}-user-logging-forward || error="yes"
  202.             fi
  203.  
  204.             # setup ufw${type}-before-logging-* chains
  205.             if ! $exe -L ufw${type}-before-logging-input -n >/dev/null 2>&1 ; then
  206.                 $exe -N ufw${type}-before-logging-input || error="yes"
  207.                 $exe -N ufw${type}-before-logging-output || error="yes"
  208.                 $exe -N ufw${type}-before-logging-forward || error="yes"
  209.                 $exe -A INPUT -j ufw${type}-before-logging-input || error="yes"
  210.                 $exe -A OUTPUT -j ufw${type}-before-logging-output || error="yes"
  211.                 $exe -A FORWARD -j ufw${type}-before-logging-forward || error="yes"
  212.             fi
  213.  
  214.             # setup ufw${type}-before-* chains
  215.             if ! $exe -L ufw${type}-before-input -n >/dev/null 2>&1 ; then
  216.                 $exe -N ufw${type}-before-input || error="yes"
  217.                 $exe -N ufw${type}-before-output || error="yes"
  218.                 $exe -N ufw${type}-before-forward || error="yes"
  219.                 $exe -A INPUT -j ufw${type}-before-input || error="yes"
  220.                 $exe -A OUTPUT -j ufw${type}-before-output || error="yes"
  221.                 $exe -A FORWARD -j ufw${type}-before-forward || error="yes"
  222.             fi
  223.             if [ -s "$RULES_PATH" ]; then
  224.                 if ! $exe-restore -n < $BEFORE_RULES ; then
  225.                     out="${out}\nProblem running '$BEFORE_RULES'"
  226.                     error="yes"
  227.                 fi
  228.             else
  229.                 out="${out}\nCouldn't find '$BEFORE_RULES'"
  230.             fi
  231.  
  232.             # setup ufw${type}-user chain
  233.             if [ -s "$USER_PATH" ]; then
  234.                 $exe -N ufw${type}-user-input || error="yes"
  235.                 $exe -N ufw${type}-user-output || error="yes"
  236.                 $exe -N ufw${type}-user-forward || error="yes"
  237.                 $exe -A ufw${type}-before-input -j ufw${type}-user-input || error="yes"
  238.                 $exe -A ufw${type}-before-output -j ufw${type}-user-output || error="yes"
  239.                 $exe -A ufw${type}-before-forward -j ufw${type}-user-forward || error="yes"
  240.                 if ! $exe-restore -n < $USER_RULES ; then
  241.                     out="${out}\nProblem running '$USER_RULES'"
  242.                     error="yes"
  243.                 fi
  244.                 # don't include the RETURN lines here, as they will
  245.                 # be in the USER_PATH file
  246.             fi
  247.  
  248.             # setup ufw${type}-after-* chains
  249.             if ! $exe -L ufw${type}-after-input -n >/dev/null 2>&1 ; then
  250.                 $exe -N ufw${type}-after-input || error="yes"
  251.                 $exe -N ufw${type}-after-output || error="yes"
  252.                 $exe -N ufw${type}-after-forward || error="yes"
  253.                 $exe -A INPUT -j ufw${type}-after-input || error="yes"
  254.                 $exe -A OUTPUT -j ufw${type}-after-output || error="yes"
  255.                 $exe -A FORWARD -j ufw${type}-after-forward || error="yes"
  256.             fi
  257.             if [ -s "$AFTER_RULES" ]; then
  258.                 if ! $exe-restore -n < $AFTER_RULES ; then
  259.                     out="${out}\nProblem running '$AFTER_RULES'"
  260.                     error="yes"
  261.                 fi
  262.             else
  263.                 out="${out}\nCouldn't find '$AFTER_RULES'"
  264.             fi
  265.  
  266.             # setup ufw${type}-after-logging-* chains
  267.             if ! $exe -L ufw${type}-after-logging-input -n >/dev/null 2>&1 ; then
  268.                 $exe -N ufw${type}-after-logging-input || error="yes"
  269.                 $exe -N ufw${type}-after-logging-output || error="yes"
  270.                 $exe -N ufw${type}-after-logging-forward || error="yes"
  271.                 $exe -A INPUT -j ufw${type}-after-logging-input || error="yes"
  272.                 $exe -A OUTPUT -j ufw${type}-after-logging-output || error="yes"
  273.                 $exe -A FORWARD -j ufw${type}-after-logging-forward || error="yes"
  274.             fi
  275.  
  276.             # now setup the REJECT chains
  277.             if ! $exe -L ufw${type}-reject-input -n >/dev/null 2>&1 ; then
  278.                 $exe -N ufw${type}-reject-input || error="yes"
  279.                 $exe -N ufw${type}-reject-output || error="yes"
  280.                 $exe -N ufw${type}-reject-forward || error="yes"
  281.                 $exe -A INPUT -j ufw${type}-reject-input || error="yes"
  282.                 $exe -A OUTPUT -j ufw${type}-reject-output || error="yes"
  283.                 $exe -A FORWARD -j ufw${type}-reject-forward || error="yes"
  284.             fi
  285.  
  286.             if [ "$DEFAULT_INPUT_POLICY" = "REJECT" ]; then
  287.                 $exe -A ufw${type}-reject-input -j REJECT || error="yes"
  288.             fi
  289.             if [ "$DEFAULT_OUTPUT_POLICY" = "REJECT" ]; then
  290.                 $exe -A ufw${type}-reject-output -j REJECT || error="yes"
  291.             fi
  292.             if [ "$DEFAULT_FORWARD_POLICY" = "REJECT" ]; then
  293.                 $exe -A ufw${type}-reject-forward -j REJECT || error="yes"
  294.             fi
  295.         done
  296.  
  297.         if [ ! -z "$IPT_SYSCTL" ] && [ -s "$IPT_SYSCTL" ]; then
  298.             sysctl -e -q -p $IPT_SYSCTL || true
  299.         fi
  300.  
  301.         if [ "$error" = "yes" ]; then
  302.             /bin/echo -e "$out"
  303.             return 1
  304.         fi
  305.     else
  306.         out="Skip starting firewall: ufw (not enabled)"
  307.     fi
  308.     if [ ! -z "$out" ]; then
  309.         /bin/echo -e "$out"
  310.     fi
  311. }
  312.  
  313. ufw_stop() {
  314.     if [ "$1" != "--force" ] && [ "$ENABLED" != "yes" ] && [ "$ENABLED" != "YES" ]; then
  315.         echo "Skip stopping firewall: ufw (not enabled)"
  316.         return 0
  317.     fi
  318.  
  319.     # If we manage the builtins, just return
  320.     if [ "$MANAGE_BUILTINS" = "yes" ]; then
  321.         flush_builtins || return 1
  322.         return 0
  323.     fi
  324.  
  325.     error=""
  326.     execs="iptables"
  327.     if ip6tables -L INPUT -n >/dev/null 2>&1; then
  328.         execs="$execs ip6tables"
  329.     fi
  330.  
  331.     for exe in $execs
  332.     do
  333.         type=""
  334.         if [ "$exe" = "ip6tables" ]; then
  335.             type="6"
  336.         fi
  337.         delete_chains $type || error="yes"
  338.         $exe -P INPUT ACCEPT || error="yes"
  339.         $exe -P OUTPUT ACCEPT || error="yes"
  340.         $exe -P FORWARD ACCEPT || error="yes"
  341.     done
  342.  
  343.     if [ "$error" = "yes" ]; then
  344.         return 1
  345.     fi
  346.     return 0
  347. }
  348.  
  349. ufw_reload() {
  350.     if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
  351.         ufw_stop || return "$?"
  352.         ufw_start || return "$?"
  353.     else
  354.         echo "Skipping $1 (not enabled)"
  355.     fi
  356.     return 0
  357. }
  358.  
  359. ufw_status() {
  360.     err=""
  361.     iptables -L ufw-user-input -n >/dev/null 2>&1 || {
  362.         echo "Firewall is not running"
  363.         return 3
  364.     }
  365.  
  366.     if [ "$IPV6" = "yes" ] || [ "$IPV6" = "YES" ]; then
  367.         ip6tables -L ufw6-user-input -n >/dev/null 2>&1 || {
  368.             # unknown state: ipv4 ok, but ipv6 isn't
  369.             echo "Firewall in inconsistent state (IPv6 enabled but not running)"
  370.             return 4
  371.         }
  372.     fi
  373.  
  374.     echo "Firewall is running"
  375.     return 0
  376. }
  377.  
  378.